package com.promote.common.properties.initializer;

import java.util.Collections;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
//import org.apache.logging.log4j.ThreadContext;
//import org.apache.logging.log4j.core.config.ConfigurationSource;
//import org.apache.logging.log4j.core.config.Configurator;
//import org.apache.logging.log4j.core.config.xml.XmlConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import com.promote.common.properties.FilePropertiesMap;
import com.promote.common.properties.service.PropertyService;
import com.promote.common.properties.service.impl.RpcPropertyServiceImpl;
/**
 * 文件初始化加载
 * @author tarena
 *
 */
public class PlatformInitFactory {
	private static Logger log = LoggerFactory.getLogger(PlatformInitFactory.class);
	public static boolean hasInit = false;

	private static PropertyService rpcPropertyService = new RpcPropertyServiceImpl();

	public static void init() {
		try {
			if (hasInit) {
				return;
			}
			hasInit = true;
			log.info("开始加载配置文件 ");
			Map<String, Map<String, String>> rpcMap = rpcPropertyService.loadProperties();
			if (CollectionUtils.isEmpty(rpcMap)) {
				return;
			}
			for (String key : rpcMap.keySet()) {
				FilePropertiesMap.addProperties(key, rpcMap.get(key));
			}
			Map<String, Map<String, String>> dataMap = Collections.emptyMap();
			String rpcModel = FilePropertiesMap.getItemValue("rpc", "rpc.model");
			if (!CollectionUtils.isEmpty(dataMap)) {
				for (String module : dataMap.keySet()) {
					Map<String, String> dMap = dataMap.get(module);
					if (CollectionUtils.isEmpty(dMap)) {
						continue;
					}
					log.info("module={}",module);
					Map<String, String> tmpMap = FilePropertiesMap.getMap(module);
					if (CollectionUtils.isEmpty(tmpMap)) {
						log.info("module={}不为空，开始加载",module);
						FilePropertiesMap.addProperties(module, dMap);
						continue;
					}
					for (String dkey : dMap.keySet()) {
						if (!tmpMap.containsKey(dkey)) {
							tmpMap.put(dkey, dMap.get(dkey));
						}
					}
				}
			}
			for (String file : FilePropertiesMap.getAllFile()) {
				if ("system".equals(file)) {
					FilePropertiesMap.SYS_MAP = FilePropertiesMap.getMap(file);
				} else if ("redis-key".equals(file)) {
					FilePropertiesMap.REDIS_MAP = FilePropertiesMap.getMap(file);
				}
			}
			String jdbcSource = FilePropertiesMap.getItemValue("jdbc", "jdbc.source");
			if (StringUtils.isEmpty(jdbcSource)) {
				String value = FilePropertiesMap.getItemValue("jdbc", "jdbc.url");
				if (!StringUtils.isEmpty(value) && value.toLowerCase().contains("mysql")) {
					FilePropertiesMap.addProperties("jdbc", "jdbc.source", "Mysql");
				} else if (!StringUtils.isEmpty(value) && value.toLowerCase().contains("oracle")) {
					FilePropertiesMap.addProperties("jdbc", "jdbc.source", "Oracle");
				}
			}
			// 设置log4j日志的存储目录
			//reloadLog4JConfig();
			log.info("配置文件加载完成 ");
		} catch (Exception e) {
			e.printStackTrace();
			log.info("load properties error:" + e.getMessage());
		}
	}

//	private static void reloadLog4JConfig() {
//		try {
//			String fileRoot = FilePropertiesMap.getValue("file.root.path");
//			String proName = FilePropertiesMap.getValue("pro.name");
//			String localIp = SystemUtil.getHostInfo().getAddress();
//			if (StringUtils.isEmpty(fileRoot)) {
//				log.info("fileRoot is empty, log4j is not reload!");
//			}
//			String logHomePath = fileRoot + "/logs/" + proName + "/" + localIp;
//			File logFilePath = new File(logHomePath);
//			if (!logFilePath.exists() || !logFilePath.isDirectory()) {
//				FileUtil.mkdir(logFilePath);
//			}
//			ThreadContext.put("log.file.path", logHomePath);
//			String fileName = FilePropertiesMap.getValue("pro.name");
//			if (!StringUtils.isEmpty(fileName)) {
//				ThreadContext.put("log.file.name", fileName);
//			}
//			String rpcActive = FilePropertiesMap.getValue("rpc.active");
//			if (StringUtils.isEmpty(rpcActive)) {
//				log.info("rpc active is empty !");
//			}
//			String relativePath = "distribute/" + rpcActive + "/log4j2.xml";
//			ClassPathResource cp = new ClassPathResource(relativePath);
//
//			if (cp.exists()) {
//				ConfigurationSource source = new ConfigurationSource(cp.getInputStream());
//				LoggerContext context = Configurator.initialize(null, source);
//				XmlConfiguration xmlConfig = new XmlConfiguration(context, source);
//				context.start(xmlConfig);
//			}
//			log.info("重载日志完成，日志目录：{} ", logHomePath);
//		} catch (IOException e) {
//			log.error("加载日志模块失败！{}", e.getMessage());
//		}
//	}
}
